Golang encoding/xml.Encoder.Flush() function example
package encoding/xml
Flush flushes any buffered XML to the underlying writer. See the EncodeToken documentation for details about when it is necessary.
Golang encoding/xml.Encoder.Flush() function usage example
package main
import (
"encoding/xml"
"fmt"
"os"
"reflect"
)
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
encoder := xml.NewEncoder(os.Stdout)
newtoken := xml.StartElement{
Name : xml.Name{
Space: "",
Local: "location",
},
}
if err := encoder.EncodeToken(newtoken); err != nil {
fmt.Println(err)
}
start := xml.StartElement{
Name: xml.Name{
Space: "",
Local: reflect.Indirect(reflect.ValueOf(v)).Type().Name(),
},
Attr: []xml.Attr{{xml.Name{"", "xmlns"}, "https://socketloop.com/xmldoc/2014-09-01/"}},
}
if err := encoder.EncodeElement(v, start); err != nil {
fmt.Println(err)
}
encoder.Flush() // <-- here
}
Reference :
Advertisement
Something interesting
Tutorials
+12.7k Golang : Sort and reverse sort a slice of bytes
+18.8k Golang : Implement getters and setters
+14.8k Golang : Get URI segments by number and assign as variable example
+19.2k Golang : Check if directory exist and create if does not exist
+10.8k Golang : Command line file upload program to server example
+10.7k Golang : Get currencies exchange rates example
+14.4k Golang : How to filter a map's elements for faster lookup
+10.9k Golang : Removes punctuation or defined delimiter from the user's input
+14.9k Golang : Submit web forms without browser by http.PostForm example
+8.3k Golang: Prevent over writing file with md5 hash
+37.5k Upload multiple files with Go
+5.6k Golang : Shortening import identifier